package com.sx.netty.netty.demo02_Heartbeat;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.timeout.IdleStateEvent;
import io.netty.util.concurrent.EventExecutorGroup;

/**
 * @Author ShuXin
 * @Description
 * @Date 2020/11/18 15:51
 */
public class MyServerHandler extends ChannelInboundHandlerAdapter{

    /**
     * 心跳机制执行部分
     * @param ctx
     * @param evt
     * @throws Exception
     */
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if(evt instanceof IdleStateEvent){
            //向下转型
            IdleStateEvent event = (IdleStateEvent)evt;
            String eventType = null;
            switch(event.state()){
                case READER_IDLE:
                    eventType = "读空闲";
                    break;
                case WRITER_IDLE:
                    eventType = "写空闲";
                    break;
                case ALL_IDLE:
                    eventType = "读写空闲";
            }
            System.out.println(ctx.channel().remoteAddress().toString().substring(1) + "--超时时间--" + eventType);
            System.out.println("服务器做相应的处理");

            //如果发生空闲，就关闭channel
            ctx.channel().close();
        }
    }
}
